package com.atguigu.servlet;

import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Date:2020/10/13
 * Author:ybc
 * Description:
 */
@WebServlet(name = "CodeVerifyServlet", value = "/CodeVerifyServlet")
public class CodeVerifyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String phone_no = request.getParameter("phone_no");
        String verify_code = request.getParameter("verify_code");
        //创建redis中存储正确验证码的键
        String codeKey = "verifyCode:"+phone_no+":code";
        //获取正确的验证码
        Jedis jedis = new Jedis("192.168.192.132", 6379);
        String code = jedis.get(codeKey);
        //判断用户输入的验证码是否正确
        if(code.equals(verify_code)){
            //响应客户端true
            response.getWriter().print(true);
        }
    }
}
